//
//  ViewController.m
//  OneToOne
//
//  Created by ByteDance on 2023/2/20.
//

#import "ViewController.h"
#import "MeetingViewController.h"
#import <Masonry.h>

static NSString *WBUserIdKey = @"ByteWBUserIdKey";
static NSString *WBRoomIdKey = @"ByteWBRoomIdKey";

@interface ViewController () <UITextFieldDelegate>
@property (nonatomic, strong) UIButton *joinRoom;
@property (nonatomic, strong) UITextField *roomField;
@property (nonatomic, strong) UITextField *userField;
@property (nonatomic, strong) UILabel *firstTitle;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.joinRoom];
    [self.view addSubview:self.roomField];
    [self.view addSubview:self.userField];
    [self.view addSubview:self.firstTitle];
    
    [self.joinRoom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).offset(50);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width / 2, 50));
    }];
    [self.userField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width - 10, 50));
        make.bottom.equalTo(self.joinRoom.mas_top).offset(-50);
    }];
    [self.roomField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width - 10, 50));
        make.bottom.equalTo(self.userField.mas_top).offset(-50);
    }];
    
    [self.firstTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width, 100));
        make.bottom.equalTo(self.roomField.mas_top).offset(-50);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
    // Do any additional setup after loading the view.
}

- (void)action {
    NSString *roomId = self.roomField.text;
    NSString *userId = self.userField.text;
    
    MeetingViewController *vc = [[MeetingViewController alloc] init];
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:vc animated:YES completion:nil];
    [[NSUserDefaults standardUserDefaults] setObject:roomId forKey:WBRoomIdKey];
    [[NSUserDefaults standardUserDefaults] setObject:userId forKey:WBUserIdKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(void)viewTapped:(UITapGestureRecognizer*)tap {
[self.view endEditing:YES];
}
#pragma mark - delegate
- (UIButton *)joinRoom {
    if (!_joinRoom) {
        _joinRoom = [[UIButton alloc] init];
        _joinRoom.backgroundColor = [UIColor blueColor];
        [_joinRoom setTitle:@"进入房间" forState:UIControlStateNormal];
        [_joinRoom setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_joinRoom setTitleColor:[UIColor brownColor] forState:UIControlEventTouchUpInside];
        [_joinRoom addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
    }
    return _joinRoom;
}
- (UITextField *)roomField {
    if (!_roomField) {
        _roomField = [[UITextField alloc] init];
        _roomField.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.9 alpha:1];
        _roomField.layer.borderWidth= 1.0;
        _roomField.borderStyle = UITextBorderStyleNone;
        _roomField.layer.borderColor= [UIColor lightGrayColor].CGColor;
        _roomField.placeholder = @"输入房间Id";
        NSString *roomId = [[NSUserDefaults standardUserDefaults] objectForKey:WBRoomIdKey];
        if (roomId) {
            _roomField.text = roomId;
        }
        _roomField.delegate = self;
    }
    return _roomField;
}
- (UITextField *)userField {
    if (!_userField) {
        _userField = [[UITextField alloc] init];
        _userField.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.9 alpha:1];
        _userField.layer.borderWidth= 1.0;
        _userField.borderStyle = UITextBorderStyleNone;
        _userField.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _userField.placeholder = @"输入用户Id";
        NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:WBUserIdKey];
        if (userId) {
            _userField.text = userId;
        }
        _userField.delegate = self;
    }
    return _userField;
}

- (UILabel *)firstTitle {
    if (!_firstTitle) {
        _firstTitle = [[UILabel alloc] init];
        _firstTitle.text = @"demo - demo - demo";
        _firstTitle.font = [UIFont systemFontOfSize:40];
        _firstTitle.textColor = [UIColor purpleColor];
    }
    return _firstTitle;
}
@end
